package com.sabm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sabm.model.Address;
import com.sabm.model.Contact;
import com.sabm.model.Email;

@Repository
public class EmailDAOImpl implements EmailDAO{

	@Autowired
    private SessionFactory sessionFactory;
	//
	public void save(Email email){
		sessionFactory.getCurrentSession().save(email);
	}
	//
	public void delete(Email email){
		sessionFactory.getCurrentSession().delete(email);
	}
	//
	public void update(Email email){
		sessionFactory.getCurrentSession().update(email);
	}
	//
	public void saveOrUpdate(Email email){
		sessionFactory.getCurrentSession().saveOrUpdate(email);
	}
	//
	public Email getEmailById(int idEmail){
		Query query = sessionFactory.getCurrentSession().createQuery("from Email where idEmail = :idEmail ");
		query.setParameter("idEmail", idEmail);
		List<Email> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
}
